package com.cie2.janji.services;

import java.util.Iterator;
import java.util.List;

import org.apache.tapestry5.ioc.annotations.Inject;
import org.hibernate.Session;

import com.cie2.janji.entities.BaseEntity;
import com.cie2.janji.entities.Category;


public class CategoryService {

	private Session session;

	@Inject
	private BaseEntityService baseEntityService;

	@Inject
	private PromiseStatisticsService statisticsService;
	
	public CategoryService(Session session) {
		this.session = session;
	}
	
	public Category load(Long categoryId) {
		return (Category) session.load(Category.class, categoryId);
	}
	
	public void add(Category category) {
		session.persist(category);
		
		List entities = baseEntityService.getAll();
		
		for (Iterator iter = entities.iterator(); iter.hasNext();) {
			BaseEntity entity = (BaseEntity) iter.next();
			
			statisticsService.createCategoryStatistics(entity, category);
		}
		
		session.flush();
		
	}

	public void update(Category category) {
		session.persist(category);
		session.flush();
	}
	
	public List getAll() {
		return session.createCriteria(Category.class).list();
	}

}
